package com.bluefeather.songlyric;

import java.util.ArrayList;

import android.app.ListFragment;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AuthorList extends ListFragment{
	
	public static String			TAG	= "AUTHOR";
	
	private ArrayList<String>		_list;
	private ArrayAdapter<String>	_adapter;

	static AuthorList newInstance() {
		AuthorList al = new AuthorList();
		return al;
	}
	
	// it seems Android will call empty constructor and use ugly setArguments/getArguments
	public AuthorList() {}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
		View view = inflater.inflate(R.layout.fragment_music_list, container, false);
		view.setBackgroundColor(0xffffffff);
		Cursor c = DB.instance().getAuthorList();
		_list = new ArrayList<String>();
		while(!c.isAfterLast()) {
			String s = c.getString(c.getColumnIndex(DB.LYRIC._AUTHOR));
			s += " (";
			s += c.getString(c.getColumnIndex(DB.LYRIC._DYNASTY));
			s += ")";
			_list.add(s);
			c.moveToNext();
		}
		_adapter = new ArrayAdapter<String>(SongLyricApp.instance(), R.layout.row_music, R.id.music_name, _list);
		setListAdapter(_adapter);
		return view;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		String s = _list.get(position);
		String author = s.substring(0, s.indexOf(' '));
		SongLyricActivity activity = (SongLyricActivity)getActivity();
		activity.openAuthor(author);
	}
	
}
